package com.soccernews.main;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;






import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Paper extends Activity {
	ListView list;
	private List<Message> messages;
	ProgressDialog pd;
	private Object data = null;
	Elements content;
	List<Bitmap> images ;
	
	private Gallery gallery;
    private ImageView imgView;

    private Integer[] Imgid = {
            R.drawable.arrow_right,R.drawable.arrow_right,R.drawable.arrow_right,R.drawable.arrow_right,R.drawable.arrow_right
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_screen);
        //this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);

    	
        new DownloadTask().execute();
        
    }
    
    private class DownloadTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args) {
            Log.i("MyApp", "Background thread starting");
            try {
            Document doc = Jsoup.connect("http://www.varzesh3.com/").get();
            Document doc2;
            images = new ArrayList<Bitmap>();
			
			 content = doc.select("div#widget-place-2");
			 content = content.select("div#identifierwidget-9");
			 content = content.select("div.widget-content");
			 content = content.select("div#showImage");
			 content = content.select("a");
		    for(int i=0;i<content.size();i++){
		    	doc2 = Jsoup.connect("http://www.varzesh3.com" + content.get(i).attr("href").substring(1)).get();
		    		try {
		    		images.add(BitmapFactory.decodeStream((
		    				InputStream)new URL(doc2.select("div#anc-op img").attr("src")).getContent()));
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    }
		     
		     
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
            return images;
        }

        protected void onPostExecute(Object result) {
            // Pass the result data back to the main activity
        	Paper.this.data = result;
        	if (images == null)
        		new DownloadTask().execute();
        	else{
        	setContentView(R.layout.paper);
        	//TextView test = (TextView)findViewById(R.id.textView1);
        	imgView = (ImageView)findViewById(R.id.ImageView01);
        	
            imgView.setImageBitmap(images.get(0));

             gallery = (Gallery) findViewById(R.id.examplegallery);
             gallery.setAdapter(new AddImgAdp(Paper.this));

             gallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position, long id) {
                	
                    imgView.setImageBitmap(images.get(position));

                	
                    //Toast.makeText(getBaseContext(),"This is image gallary"+content.get(position), Toast.LENGTH_LONG).show();
                }
            });
        	
				
   		}	
        	if (Paper.this.pd != null) {
            	Paper.this.pd.dismiss();
            }
        }
    }
    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return images.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);
            
            
            imgView.setImageBitmap(images.get(position));

           
            imgView.setLayoutParams(new Gallery.LayoutParams(150, 140));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);
            return imgView;
        }
    } 

   
}